# -*- encoding: utf-8 -*-
"""
@File    : ip.py
@Time    : 2020/10/27 21:08
@Author  : lluyao
@Email   : lluyao61@163.com
@Software: PyCharm
"""
import random

def create_file(filename:str) -> None:
    with open(filename, 'w', encoding='utf-8') as f:
        for i in range(120000):
            f.write(f'192.168.254.{random.randint(1, 255)}\n')

def counter_top(filename:str, top=10) -> list:
    # key=> ip, value => 出现的次数
    result = dict()
    with open(filename, mode="r", encoding="utf-8") as f:
        for ip in f:
            ip = ip.strip()
            if ip in result:
                result[ip] += 1
            else:
                result[ip] = 1
    # 排序 =》
    # [(key,value), (key,value)]
    return sorted(result.items(), key=lambda x:x[1], reverse=True)[:top]



filename = "ips.txt"
create_file(filename)
result = counter_top(filename)
print(result)